package com.huikeportal.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.huike.model.MsgResponse;
import com.huike.tools.DateTool;
import com.huike.tools.MessageTool;
import com.huikeportal.model.ChainSiteInfo;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.SiteCommon;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.common.InterFaceModel;
import com.huikeportal.service.InterFaceCommon;
import com.huikeportal.service.LoginService;
import com.huikeportal.service.SelectChainSiteService;
import com.huikeportal.service.SelectSiteService;

@Controller
public class SiteAPI {

	@Autowired
	private InterFaceCommon interFaceCommon;

	@Autowired
	private LoginService loginService;

	@Autowired
	private SelectSiteService selectSiteService;

	@Autowired
	private SelectChainSiteService selectChainSiteService;

	/**
	 * 获取用户场所权限（集团、广场）
	 * 
	 * @param userId
	 *            用户编号
	 * @param type
	 *            1：只获取集团、2：集团+广场、3：广场、4：连锁店店铺
	 * @return
	 */
	@RequestMapping(value = "/huike_getSiteByUser", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getSiteByUser(String userId, String type) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getSiteByUser");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userId == null || "".equals(userId) || type == null
				|| "".equals(type)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		UserInfo userInfo = new UserInfo();
		userInfo.setUserId(userId);
		try {
			// 如果是查询广场
			if ("3".equals(type)) {
				List<SiteAllInfo> gcList = loginService.getGcSiteList(userInfo);
				msgResponse.setObject(gcList);
				msgResponse.setRespCode("90001");
				msgResponse.setRespCodeDes(MessageTool.RESP_90001);
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				interFaceModel.setInterStatus("success");
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			}
			// 如果是查询集团
			else if ("1".equals(type)) {
				List<SiteAllInfo> jtList = loginService.getJtSiteList(userInfo);
				msgResponse.setObject(jtList);
				msgResponse.setRespCode("90001");
				msgResponse.setRespCodeDes(MessageTool.RESP_90001);
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				interFaceModel.setInterStatus("success");
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			}
			// 如果是查询集团+广场
			else if ("2".equals(type)) {
				List<SiteAllInfo> jtGcList = new ArrayList<SiteAllInfo>();
				List<SiteAllInfo> gcList = loginService.getGcSiteList(userInfo);
				List<SiteAllInfo> jtList = loginService.getJtSiteList(userInfo);
				jtGcList.addAll(jtList);
				jtGcList.addAll(gcList);
				msgResponse.setObject(jtGcList);
				msgResponse.setRespCode("90001");
				msgResponse.setRespCodeDes(MessageTool.RESP_90001);
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				interFaceModel.setInterStatus("success");
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			}
			// 如果是查询连锁店店铺
			else if ("4".equals(type)) {
				List<ChainSiteInfo> chainList = selectChainSiteService
						.queryChainList(userId);
				msgResponse.setObject(chainList);
				msgResponse.setRespCode("90001");
				msgResponse.setRespCodeDes(MessageTool.RESP_90001);
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				interFaceModel.setInterStatus("success");
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			} else {
				msgResponse.setRespCode("90003");
				msgResponse.setRespCodeDes(MessageTool.RESP_90003);
				interFaceModel.setEndTime(DateTool.getDateYmdHms());
				interFaceModel.setInterStatus("success");
				interFaceCommon.logInterface(interFaceModel);
				return JSONObject.fromObject(msgResponse).toString();
			}
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 根据场所id查询场所信息
	 * 
	 * @param siteId
	 * @return
	 */
	@RequestMapping(value = "/huike_getSiteInfo", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getSiteInfo(String siteId) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getSiteInfo");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (siteId == null || "".equals(siteId)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			SiteCommon siteCommon = selectSiteService.querySiteInfo(siteId);
			msgResponse.setObject(siteCommon);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	@RequestMapping(value = "/huike_getChainListByCondition", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getChainListByCondition(String userId, String diqu,
			String jxs, String dplx) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getChainListByCondition");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userId == null || "".equals(userId)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			ChainSiteInfo chainSiteInfo = new ChainSiteInfo();
			chainSiteInfo.setUserId(userId);
			if (diqu != null && !"".equals(diqu)) {
				diqu = URLDecoder.decode(diqu,"utf-8");
				chainSiteInfo.setParent200Name(diqu);
			}
			if (jxs != null && !"".equals(jxs)) {
				jxs = URLDecoder.decode(jxs,"utf-8");
				chainSiteInfo.setDealer(jxs);
			}
			if (dplx != null && !"".equals(dplx)) {
				dplx = URLDecoder.decode(dplx,"utf-8");
				chainSiteInfo.setStoreType(dplx);
			}
			List<ChainSiteInfo> chainList = selectChainSiteService
					.getChainList(chainSiteInfo);
			msgResponse.setObject(chainList);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}
}
